// -----------------------------------------------------------------------
//  <copyright file="EntityFrameworkCoreModule.cs" company="LiuliuSoft">
//      Copyright (c) 2022-2022 DaprPlus. All rights reserved.
//  </copyright>
//  <site>https://www.dapr.plus</site>
//  <last-editor>郭明锋</last-editor>
//  <last-date>2022-11-19 2:01</last-date>
// -----------------------------------------------------------------------

using DaprPlus.AspNetCore.Mvc.Filters;


namespace DaprPlus.EntityFrameworkCore;


public abstract class EntityFrameworkCoreModuleBase : ModuleBase
{
    /// <summary>
    /// 获取 模块级别，级别越小越先启动
    /// </summary>
    public override ModuleLevel Level => ModuleLevel.Framework;

    /// <summary>
    /// 将模块服务添加到依赖注入服务容器中
    /// </summary>
    /// <param name="services">依赖注入服务容器</param>
    /// <returns></returns>
    public override IServiceCollection AddServices(IServiceCollection services)
    {
        services.AddEntityFrameworkCore();
        services.AddSingleton<IApiResultExceptionHandler, ForeignKeyConstraintExceptionHandler>();
        return base.AddServices(services);
    }

    /// <summary>
    /// 应用模块服务
    /// </summary>
    /// <param name="provider">服务提供者</param>
    public override async Task UseModule(IServiceProvider provider)
    {
        await provider.CheckAndSeedAsync();
        await base.UseModule(provider);
    }
}
